package com.tssf.car_manage.controller;

import com.tssf.car_manage.entity.Admin;
import com.tssf.car_manage.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    // 新增搜索方法
    @GetMapping("/search")
    public String searchAdmins(
            @RequestParam String key,
            Model model,
            RedirectAttributes redirectAttributes) {

        try {
            List<Admin> admins = adminService.searchAdmins(key);
            model.addAttribute("admins", admins);
            model.addAttribute("key", key); // 保留搜索关键词

            if (admins.isEmpty()) {
                redirectAttributes.addFlashAttribute("info", "未找到匹配的管理员");
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "搜索失败: " + e.getMessage());
        }

        return "admin/list";
    }

    @GetMapping("/list")
    public String list(Model model) {
        List<Admin> adminList = adminService.getAllAdmins();
        model.addAttribute("admins", adminList);
        return "admin/list";
    }


    @GetMapping("/add")
    public String addForm(Model model) {
        model.addAttribute("admin", new Admin());
        return "admin/add";
    }

//    @PostMapping("/save")
//    public String save(Admin admin) {
//        adminService.addAdmin(admin);
//        return "redirect:/admin/list";
//    }

    @PostMapping("/save")
    public String save(Admin admin, RedirectAttributes attributes) {
        try {
            if (admin.getId() == null) {
                // 新增操作，密码必须填写
                if (admin.getPassword() == null || admin.getPassword().trim().isEmpty()) {
                    attributes.addFlashAttribute("error", "新增管理员必须设置密码");
                    return "redirect:/admin/add";
                }
                adminService.addAdmin(admin);
                attributes.addFlashAttribute("message", "添加成功");
            } else {
                // 编辑操作，处理密码留空情况
                Admin existingAdmin = adminService.getAdminById(admin.getId()).get(0);
                if (admin.getPassword() == null || admin.getPassword().trim().isEmpty()) {
                    // 保留原密码
                    admin.setPassword(existingAdmin.getPassword());
                }
                adminService.updateAdmin(admin);
                attributes.addFlashAttribute("message", "更新成功");
            }
        } catch (Exception e) {
            attributes.addFlashAttribute("error", "操作失败: " + e.getMessage());
        }
        return "redirect:/admin/list";
    }

    @GetMapping("/edit/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) {
        List<Admin> admins = adminService.getAdminById(id);
        if (!admins.isEmpty()) {
            model.addAttribute("admin", admins.get(0));
        }
        return "admin/edit";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        adminService.deleteAdmin(id);
        return "redirect:/admin/list";
    }


}